package com.ijava.base.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ijava.base.domain.DictionaryDefine;
import com.ijava.base.domain.DictionaryDetail;
import com.ijava.base.domain.User;
import com.ijava.base.service.DictionaryDefineService;
import com.ijava.base.service.DictionaryDetailService;
import com.ijava.util.DTreeBean;

@Controller
@RequestMapping("/admin/dictionaryDetail.do")
@SessionAttributes("list")
public class DictionaryDetailController {
	private static final Logger logger = LoggerFactory.getLogger(DictionaryDetailController.class);
	@Autowired
	private DictionaryDetailService service;
	@Autowired
	private DictionaryDefineService defineService;
	
	@RequestMapping(params = "action=tree")
	public String tree(ModelMap modelMap, DictionaryDefine dictionaryDefine, HttpSession session)
			throws Exception {
		try {
			modelMap.addAttribute("defineId", dictionaryDefine.getDefineId());
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/dictionary/dictionaryDetailTreeIndex.jsp";
	}
	
	@RequestMapping(params = "action=subTree")
	public String subTree(ModelMap modelMap, DictionaryDetail dictionaryDetail, HttpSession session,HttpServletRequest request)
			throws Exception {
		try {
			String context = request.getContextPath(); //session.getServletContext().getContextPath();
			List tree = new ArrayList();
			
			List dTreeList = new ArrayList();
			DictionaryDefine define = defineService.get(dictionaryDetail.getDetailId());
			dTreeList.add(new DTreeBean(define.getDefineId(), "-1", define.getDefineName(),
					context+"/admin/dictionaryDetail.do?action=list&detailLevel=1&detailId="+define.getDefineId(),
					define.getDefineName(),"treeListFrame"));
			
			List list = service.findTree(dictionaryDetail.getDetailId(),tree);
			for (int i = 0; i < list.size(); i++) {
				
//				<c:url value="/admin/dictionaryDetail.do?action=list&detailId=${dTree.id}"/>
				DictionaryDetail detail = (DictionaryDetail)list.get(i);
				DTreeBean dTree = new DTreeBean(detail.getDetailId(),
						detail.getDetailParentId(),
						detail.getDetailName(),
						context+"/admin/dictionaryDetail.do?action=list&detailId="+detail.getDetailId()+"&detailLevel="+detail.getDetailLevel(),
						detail.getDetailName(),"treeListFrame");
				dTreeList.add(dTree);
			}
			modelMap.addAttribute("list", dTreeList);
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/dictionary/dictionaryDetailTreeLeft.jsp";
	}

	@RequestMapping(params = "action=list")
	public String list(ModelMap modelMap, DictionaryDetail dictionaryDetail, HttpSession session)
			throws Exception {
		try {
			List list = service.findWithPaging(dictionaryDetail);
			modelMap.addAttribute("list", list);
			modelMap.addAttribute("command",dictionaryDetail );
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/dictionary/dictionaryDetailList.jsp";
	}
	
	@RequestMapping(params = "action=ajaxRemove")
	public void ajaxRemove(ModelMap modelMap, String detailId, HttpSession session,HttpServletResponse response)
			throws Exception {
		String res = "false";
		try {
			List list = new ArrayList();
			// can rewrite to hasChildren
			service.findTree(detailId, list);
			if(list.size() == 0){
				// has child node
				if(!"_".equals(detailId)){
					service.removeProcess(service.get(detailId));
				}
			}else{
				res = "true";
			}
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(res);
	}
	
	@RequestMapping(params = "action=common")
	public String common(ModelMap modelMap, DictionaryDefine dictionaryDefine, HttpSession session)
			throws Exception {
		try {
			modelMap.addAttribute("defineId", dictionaryDefine.getDefineId());
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/dictionary/dictionaryDetailCommonIndex.jsp";
	}
	
	@RequestMapping(params = "action=commonDictTop")
	public String commonDictTop(ModelMap modelMap, DictionaryDefine dictionaryDefine, HttpSession session,HttpServletRequest request)
			throws Exception {
		try {
			modelMap.addAttribute("command", defineService.get(dictionaryDefine.getDefineId()));
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/dictionary/dictionaryDetailCommonTop.jsp";
	}
	
	@RequestMapping(params = "action=saveEdit")
	public String saveEdit(ModelMap modelMap, DictionaryDetail dictionaryDetail, HttpSession session)
			throws Exception {
		try {
			User loginUser = (User) session.getAttribute("loginUser");
			service.saveAll(dictionaryDetail, loginUser);
		} catch (Exception e) {
			logger.error("", e);
			throw e;
		}
		return list(modelMap, dictionaryDetail, session);
	}
}

